// pages/refundApply/refundApply.js
import * as API_Order from '../../api/order'
import * as API_Product from '../../api/product'
import * as API_Store from '../../api/store'
import * as API_Dict from '../../api/dict'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {},
    store: {},
    product: {},
    cause: [],
    causeIndex: 0,
    refund: {
      cause: '',
      orderId: ''
    },
    commitState: true
  },

  changeCause(e) {
    let item = this.data.cause[e.detail.value]
    this.setData({
      causeIndex: e.detail.value,
      'refund.cause': item.dictLabel
    })
  },
  apply(e) {
    API_Order.refundApply(this.data.refund).then(res => {
      console.log(res)
      if (res.code == 200) {
        var page = getCurrentPages();
        var prevPage = page[page.length - 3]
        wx.navigateBack({
          delta: 2,
          success(e) {
            prevPage.flush()
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    API_Order.getOrder(options.orderId).then(res => {
      if (res.data) {
        this.setData({
          order: res.data,
          'refund.orderId': options.orderId
        })
        getApp().https.ajax({
          method: 'post',
          url: '/product/getInfo',
          data: {
            id: res.data.productId,
            storeId: res.data.storeId
          },
          loading: true
        }).then(res => {
          that.setData({
            product: res.data
          })
        })
        API_Store.getInfo(res.data.storeId).then(res => {
          that.setData({
            store: res.data
          })
        })
        API_Dict.getDictData('refund_cause').then(res => {
          that.setData({
            cause: res,
            'refund.cause': res[0].dictLabel
          })
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})